(function(){
    "use strict";
    var dataShowController=function ($scope,$http,$modal,$location,toolsService,datashow_Service){

    $scope.timedimensions = ["最近1周","最近2周","最近1个月"];
    $scope.selectedCodeSummaryTM = "最近1周";
    $scope.requirementsInfo = {};
    $scope.processInfo = {};
    $scope.codeSummary = '';
    $scope.testProcess = {};
    $scope.tarFilesInfo = [];
    $scope.allProExistInfo = {};

    $scope.projectList = [];

    //修改项目的动作
    $scope.projectChange = function (){
        $scope.clearTable(["compilecount","compilefailcount","staticcodeanalysis","utcovery","codesubmit","caseanalysis","buganalysis"]);
        if($scope.selectedProject != undefined){
            //赋值项目ID
            $scope.projectId = $scope.selectedProject.id;
        }

        $scope.requirementsInfoGet();
        $scope.processinfoGet();
        $scope.codesubmitionInfoGet();
        $scope.testprocessinfoGet();
        // $scope.onlinedeliverinfoGet(1);
        // $scope.checkdocGet();
    };

    //获取所有的项目信息
    $scope.prosInfoGet = function(){
        //localstorage
        dataShow_getProject();
        var data = JSON.parse(localStorage.getItem("dataShow_project"));
        if(data != null && data.result != null && data.result != undefined){
            $scope.projectList = data.result;
            if ($scope.projectList != null && $scope.projectList.length > 0){
                 //赋值项目ID
                $scope.selectedProject = $scope.projectList[0];
                $scope.projectId = $scope.selectedProject.id;

                $scope.requirementsInfoGet();
                $scope.processinfoGet();
                $scope.codesubmitionInfoGet();
                $scope.testprocessinfoGet();
                // $scope.onlinedeliverinfoGet($scope.page);
                // $scope.checkdocGet();
            }
        }

       //  datashow_Service.findProject({"keyword":""}).success(function(data){
       //     if(data.result != null && data.result != undefined){
       //         $scope.projectList = data.result;
       //         if ($scope.projectList != null && $scope.projectList.length > 0){
       //              //赋值项目ID
       //             $scope.selectedProject = $scope.projectList[0];
       //             $scope.projectId = $scope.selectedProject.id;
       //
       //             $scope.requirementsInfoGet();
       //             $scope.processinfoGet();
       //             $scope.codesubmitionInfoGet();
       //             $scope.testprocessinfoGet();
       //             $scope.onlinedeliverinfoGet($scope.page);
       //             $scope.checkdocGet();
       //         }
       //     }
       // });
    };

    //获取需求总数，以及显示最新的前10条未完成的需求信息
    $scope.requirementsInfoGet = function(){
        dataShow_getRequirement($scope.selectedProject.projectName);
        var data = JSON.parse(localStorage.getItem("dataShow_Requirement"));
        $scope.requirementsInfo = data.result;
       // if($scope.projectId == '0'){
       //     return;
       // }else{
       //     datashow_Service.requirementsInfoGet({projectId : $scope.projectId}).success(function(data){
       //         if(true){
       //             $scope.requirementsInfo = data.result;
       //         }
       //     });
       // }
    };

    //将时间度量名称转化成状态码
    $scope.tmCodeGet = function(para){
        if(para == "最近1周"){
            return "week";
        }else if(para == "最近2周"){
            return "two_weeks";
        }else if(para == "最近1个月"){
            return "month";
        }else{
            return "week";
        }
    };

    //根据项目ID获取当前1周（2周或1个月，可选）的各个产品开发代码构建情况
    $scope.processinfoGet = function(){
        //localstorage
        dataShow_xfgc($scope.selectedProject.projectName);
        var data = JSON.parse(localStorage.getItem("dataShow_yfgc"));
        $scope.processInfo = data.result;
        $scope.initTableForProcess(data.result.compileInfoList,data.productKey);

       // if($scope.projectId == '0'){
       //     return;
       // }else{
       //     var para = {
       //                 "projectId" : $scope.projectId,
       //                 "timeDimension" : $scope.tmCodeGet($scope.selectedProcessTM)
       //                };
       //     datashow_Service.processinfoGet(para).success(function(data){
       //         if(true){
       //             $scope.processInfo = data.result;
       //             $scope.initTableForProcess(data.result.compileInfoList);
       //         }
       //     });
       // }
    };

    //研发情况画柱状图
    $scope.initTableForProcess = function(codeInfoList,productKey){
        if(codeInfoList == null || codeInfoList == undefined || codeInfoList.length==0){
            return;
        }

        var compileCountX = [];
        var compileCountDate = [];
        var compileCountTitle = "构建次数";
        var compileFailCountX = [];
        var compileFailCountDate = [];
        var compileFailCountTitle = "编译失败次数";
        var staticcodeanalysisX = [];
        var highData = [];
        var mediumData = [];
        var bugData =[];
        var staticcodeanalysisTitle = "静态代码分析";
        var utcoveryX = [];
        var conditionalNum = [];
        var methodNum = [];
        var statementNum = [];
        var utcoveryTitle = "测试覆盖率";
        angular.forEach(codeInfoList,function (item) {
            compileCountX.push(item.month) ;
            compileCountDate.push(item.compileNum);

            compileFailCountX.push(item.month);
            compileFailCountDate.push(item.compileFailNum);

            staticcodeanalysisX.push(item.month);
            highData.push(item.highData);
            mediumData.push(item.mediumData);
            bugData.push(item.bugData);

            utcoveryX.push(item.month);
            conditionalNum.push(item.conditionalNum);
            methodNum.push(item.methodNum);
            statementNum.push(item.statementNum);
        });
        var compileCountDataList = [];
        compileCountDataList.push({name:"失败",data:compileCountDate});
        compileCountDataList.push({name:"成功",data:compileFailCountDate});

        $("#compilecount").highcharts(bar(compileCountTitle, compileCountX, compileCountDataList,"area"));

        var utcoveryDataList = [];
        utcoveryDataList.push({name:"conditional",data:conditionalNum});
        utcoveryDataList.push({name:"method",data:methodNum});
        utcoveryDataList.push({name:"statement",data:statementNum});
        $("#dycs_bar").highcharts(bar(utcoveryTitle, utcoveryX, utcoveryDataList,"line"));

        var staticcodeanalysisDataList = [];
        staticcodeanalysisDataList.push({name:"high",data:highData});
        staticcodeanalysisDataList.push({name:"medium",data:mediumData});
        staticcodeanalysisDataList.push({name:"low",data:bugData});
        $("#zyjk_bar").highcharts(bar(staticcodeanalysisTitle, staticcodeanalysisX, staticcodeanalysisDataList,"bar"));
    };

    //清空画布内容的方法
    $scope.clearTable = function (divIds){
        // angular.forEach(divIds,function (item) {
        //     var div =$(item);
        //     div.highcharts(bar("",[],[],250,323));
        // });
        $("#compilecount").highcharts(bar("",[],[],200,323));
        $("#compilefailcount").highcharts(bar("",[],[],200,323));
        $("#staticcodeanalysis").highcharts(bar("",[],[],200,323));
        $("#utcovery").highcharts(bar("",[],[],200,323));
        $("#codesubmit").highcharts(bar("",[],[],200,323));
        $("#caseanalysis").highcharts(bar("",[],[],200,323));
        $("#buganalysis").highcharts(bar("",[],[],200,323));
    };

    //研发情况项目的时间度量change事件
    $scope.processTMChange = function (){
        //localstorage
        dataShow_xfgc($scope.selectedProject.projectName,$scope.tmCodeGet($scope.selectedProcessTM));
        var data = JSON.parse(localStorage.getItem("dataShow_yfgc"));
        $scope.processInfo = data.result;
        $scope.initTableForProcess(data.result.compileInfoList,data.productKey);
        // var para = {
        //             "projectId" : $scope.projectId,
        //             "timeDimension" : $scope.tmCodeGet($scope.selectedProcessTM)
        //            };
        // datashow_Service.processinfoGet(para).success(function(data){
        //     if(data.result != null && data.result != undefined){
        //         $scope.processInfo = data.result;
        //         // $scope.clearTable(["compilecount","compilefailcount","staticcodeanalysis","utcovery"]);
        //         $scope.initTableForProcess(data.result.compileInfoList);
        //     }
        // });
    };

    //根据下当前一周（二周或者一个月，可选）的各个产品开发的代码提交量
    $scope.codesubmitionInfoGet = function (){
        dataShow_dmzl($scope.selectedProject.projectName);
        var data = JSON.parse(localStorage.getItem("dataShow_dmzl"));
        $scope.processInfo = data.result;
        $scope.initTableForCodeSummary(data.result.codeSubmitionList,data.productKey);
        // if($scope.projectId == '0'){
        //     return;
        // }else{
        //     var para = {
        //                 "projectId" : $scope.projectId,
        //                 "timeDimension" : $scope.tmCodeGet($scope.selectedCodeSummaryTM)
        //                };
        //     datashow_Service.codesubmitionInfoGet(para).success(function(data){
        //         if(data.result != null){
        //             $scope.codeSummary = data.result.totalCodeNum;
        //             $scope.initTableForCodeSummary(data.result.codeSubmitionList);
        //         } else {
        //             $scope.codeSummary = 0;
        //         }
        //     });
        // }

    };

    //代码总量画柱状图
    $scope.initTableForCodeSummary = function(codeInfoList,productKey){
        if(codeInfoList == null || codeInfoList == undefined || codeInfoList.length==0){
            return;
        }

        var codeSubmitX = [];
        var codeSubmitDataList = [];
        angular.forEach(codeInfoList,function (item) {
            codeSubmitX.push(item.month);
            codeSubmitDataList.push(item.codeSubmitNum);
        });
        var series = [];
        series.push({name:productKey,data:codeSubmitDataList});
        var codeSubmitTitle = "代码提交量";
        $("#codesubmit").highcharts(bar(codeSubmitTitle, codeSubmitX, series,"bar"));
    };

    //代码总量项的时间度量change事件
    $scope.codeSummaryTMChange =  function (){

        // var para = {
        //             "projectId" : $scope.projectId,
        //             "timeDimension" : $scope.tmCodeGet($scope.selectedCodeSummaryTM)
        //            };
        // datashow_Service.codesubmitionInfoGet(para).success(function(data){
        //     if(data.result != null){
        //         $scope.codeSummary = data.result.totalCodeNum;
        //         // $scope.clearTable(["codesubmit"]);
        //         $scope.initTableForCodeSummary(data.result.codeSubmitionList);
        //     }
        // });
    };

    //根据项目ID获取在线测试部分当前1周（2周，1月，可选）的测试用例的执行和BUG的发现情况
    $scope.testprocessinfoGet = function (){
        //localstorage
        dataShow_zxce($scope.selectedProject.projectName);
        var data = JSON.parse(localStorage.getItem("dataShow_zxcs"));
        $scope.processInfo = data.result;
        $scope.initTableForTestProcess(data.result.doneTestCaseANDJIRANumList,data.productKey);

       // if($scope.projectId == '0'){
       //     return;
       // }else{
       //     if($scope.selectedTestProcessTM == undefined){
       //         $scope.selectedTestProcessTM = $scope.timedimensions[0];
       //     }
       //     var para = {
       //                 "projectId" : $scope.projectId,
       //                 "timeDimension" : $scope.tmCodeGet($scope.selectedTestProcessTM)
       //                };
       //     datashow_Service.testprocessinfoGet(para).success(function(data){
       //         if(data.result != null && data.result != undefined){
       //             $scope.testProcess = data.result;
       //             $scope.initTableForTestProcess(data.result.doneTestCaseANDJIRANumList);
       //         }
       //     });
       // }
    };

    //在线测试项画柱状图
    $scope.initTableForTestProcess = function(codeInfoList,productKey){
        if(codeInfoList.length==0){
            return;
        }

        var caseanalysisX = [];
        var caseanalysisData = [];
        var caseanalysisTitle = "执行用例数";
        var buganalysisX = [];
        var buganalysisData = [];
        var buganalysisTitle = "BUG数";
        var bugOpenX = [];
        var bugOpenData = [];
        var bugOpenTitle = "打开BUG数";
        var bugReopenX = [];
        var bugReopenData = [];
        var bugReopenTitle = "重新打开BUG数";
        var bugCloseX = [];
        var bugCloseData = [];
        var bugCloseTitle = "关闭BUG数";
        angular.forEach(codeInfoList,function (item) {
            caseanalysisX.push(item.month) ;
            caseanalysisData.push(item.doneTestCaseNum);

            buganalysisX.push(item.month) ;
            buganalysisData.push(item.increaseJIRANum);

            bugOpenX.push(item.month);
            bugOpenData.push(item.openJIRANum);

            bugReopenX.push(item.month);
            bugReopenData.push(item.reopenJIRANum);

            bugCloseX.push(item.month);
            bugCloseData.push(item.closeJIRANum);
        });
        // var caseanalysisDataList = [];
        // caseanalysisDataList.push({name:productKey,data:caseanalysisData});
        var buganalysisDataList = [];
        buganalysisDataList.push({name:"新增bug",data:buganalysisData});
        buganalysisDataList.push({name:"open bug",data:bugOpenData});
        buganalysisDataList.push({name:"reopen bug",data:bugReopenData});
        buganalysisDataList.push({name:"close bug",data:bugCloseData});
        // var chart1 = bar(caseanalysisTitle, caseanalysisX, caseanalysisDataList,200,280);
        var chart2 = bar(buganalysisTitle, buganalysisX, buganalysisDataList,"column");
        $("#caseanalysis").highcharts(chart2);
        // $("#bugOpen").highcharts(bar(bugOpenTitle, bugOpenX, bugOpenDataList,200,280));
        // $("#bugReopen").highcharts(bar(bugReopenTitle, bugReopenX, bugReopenDataList,200,280));
        // $("#bugClose").highcharts(bar(bugCloseTitle, bugCloseX, bugCloseDataList,200,280));
    };

    //在线测试项的时间度量change事件
    $scope.testProcessTMChange =  function (){
        var para = {
                    "projectId" : $scope.projectId,
                    "timeDimension" : $scope.tmCodeGet($scope.selectedTestProcessTM)
                   };
        datashow_Service.testprocessinfoGet(para).success(function(data){
            if(true){
                $scope.testProcess = data.result;
                // $scope.clearTable(["caseanalysis","buganalysis"]);
                $scope.initTableForTestProcess(data.result.doneTestCaseANDJIRANumList,data.productKey);
            }
        });
    };

    //上线交付项的分页显示
    //分页参数
    $scope.currentPageNum=10;
    $scope.totalItems={};
    //每页显示条数(默认10条)
    $scope.count=10;

    $scope.page = 1;
    $scope.tarAllFilesInfo = [];

    $scope.onlinedeliverinfoGet = function (page){
        $scope.tarFilesInfo = [];
        $scope.tarAllFilesInfo = [];
        var para = {
            "projectId" : $scope.projectId,
            "startIndex" : (page - 1) * $scope.count,
            "count" : $scope.count
        };
        datashow_Service.onlinedeliverinfoGet(para).success(function(data){
            if(data.result != null && data.result.length > 0){
                var tarFilesInfo = data.result;
                /*var tarFilesInfo = [
                    {   bdfProduct:{product_name:123},
                        versionList:["V1.1.0","V1.1.1","V1.1.2",
                            "V1.1.3","V1.1.4","V1.1.5","V1.1.6",
                            "V1.1.7","V1.1.8","V1.1.9","V1.1.10",
                            "V1.1.3","V1.1.4","V1.1.5","V1.1.6",
                            "V1.1.7","V1.1.8","V1.1.9","V1.1.10",
                            "V1.1.3","V1.1.4","V1.1.5","V1.1.6",
                            "V1.1.7","V1.1.8","V1.1.9","V1.1.10",
                            "V1.1.3","V1.1.4","V1.1.5","V1.1.6",
                            "V1.1.7","V1.1.8","V1.1.9","V1.1.10",
                            "V1.1.3","V1.1.4","V1.1.5","V1.1.6",
                            "V1.1.7","V1.1.8","V1.1.9","V1.1.10",
                            "V1.1.3","V1.1.4","V1.1.5","V1.1.6",
                            "V1.1.7","V1.1.8","V1.1.9","V1.1.10",
                            "V1.1.11"]
                    }
                ];*/
                if (tarFilesInfo != null && tarFilesInfo.length > 0){
                    angular.forEach(tarFilesInfo,function (item) {
                        var versionList = item.versionList;
                        if (versionList != null && versionList.length > 0){
                            angular.forEach(versionList,function (version) {
                                var tarFile = {
                                    proName : item.productKey,
                                    version : version,
                                    isExist : true
                                };
                                $scope.tarAllFilesInfo.push(tarFile);
                            });
                            $scope.tarFilesInfo = $scope.tarAllFilesInfo.slice(0,10);
                        } else {
                            $scope.tarFilesInfo.push({proName:item.productKey,isExist:false});
                        }
                    });
                    $scope.totalItems = $scope.tarAllFilesInfo.length;
                } else {
                    $scope.totalItems = 0;
                }
            }
        });
    };

    $scope.onlinedeliverinfoChange = function () {
        var beginIndex = ($scope.page - 1) * $scope.count;
        var endIndex = Number(beginIndex + $scope.count);

        if (endIndex > $scope.totalItems){
            endIndex = $scope.totalItems;
        }
        $scope.tarFilesInfo = [];
        $scope.tarFilesInfo = $scope.tarAllFilesInfo.slice(beginIndex,endIndex);
    };

    //项目验收部分的显示
    $scope.checkdocGet = function (){
        if($scope.projectId == '0'){
            return;
        }else{
            var para = {
                        "projectId" : $scope.projectId
                       };
            datashow_Service.checkdocGet(para).success(function(data){
                if (data.result.length > 0 ){
                    $scope.allProExistInfo = data.result;
                } else {
                    $scope.allProExistInfo = [];
                }
            });
        }
    };








        //获取所有的项目信息
        $scope.prosInfoGet();






    };

    angular.module("cicd.datashow",[])
            .controller("DataShowController",["$scope","$http","$modal","$location","toolsService","datashow_Service",dataShowController])
            .service("toolsService",[function () {
                        "use strict";
                        /**
                         * 获取网址上的参数
                         * @param name  网址上的参数.例如（id）
                         * @param url   网址连接。
                         * @returns {*}  返回网址参数的值
                         */
                        this.parameter = function (name, url) {
                            var parameterString = null;
                            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                            parameterString = url.substr(url.indexOf("?"), url.length);//获取截取？以后的字符串
                            var r = parameterString.substr(1).match(reg);
                            if (r != null){
                                return  unescape(r[2]);
                            }
                            return null;
                        };
                    }])

})();